/*****************************************************************************
  visualSTATE Main Loop Source File (sample code)
 
  The file contains an implementation for a main loop using the visualSTATE
  basic API. The visualSTATE project used is the AVSystem_impl example located in
  the Examples\ subdirectory of the visualSTATE installation path.
  
  The code is intended to be generic and independent of specific hardware, and 
  should be possible to compile with any ANSI-C compliant compiler.
  The code relies on stdio calls printf/scanf for user interaction in a console
  environment, such as a debugger/simulator/PC command shell.
  All the provided examples for different EW targets, VisualStudio and Cygwin, 
  except the VisualStudio GUI example all use this file for user interaction.

  The code uses a generic example queue for storing events. The functions for 
  interfacing to the queue are described in the sample code file 
  simpleEventHandler.h.
 
*****************************************************************************/

/* *** include directives *** */

#include <stdio.h>
#include <stdlib.h>
#include "SystemLight.h"


/*  This file is included in order to use an event queue with static size. */
#include "simpleEventHandler.h"

/* *** macro definitions *** */

/** Operating system visualSTATE process. Mimics a OS process for a 
    visualSTATE system. In this implementation this is the mainloop inter-
    facing to the visualSTATE basic API. */
void OS_VS_Process (void);

/* *** variable definitions *** */


/** The main event processing loop. The naming "OS_VS_Process" is arbitrary and has no
    special meaning. It merely reflects the possibility to use this function as 
    an OS process/task in an RTOS **/
void OS_VS_Process (void)
{
    /* Define and initialize. In this case the reset event is SE_RESET. */
    SEM_EVENT_TYPE eventNo = SE_RESET;

    /* Initialize the VS System. */
    SystemLightVSInitAll();
    SystemLightSEM_InitSignalQueue();

    SEQ_Initialize();

    if (SEQ_AddEvent(SE_RESET) != UCC_OK)
    {
        return;
    }

    /* Do forever */
    while(1) 
    {
        /* Get next event from queue */	  
        if (SEQ_RetrieveEvent(&eventNo) != UCC_QUEUE_EMPTY)
        {
        /* Deduct the event. */
            if (SystemLightVSDeduct(eventNo) != SES_OKAY)
            {
                return;
            }
        }
    }
}
